package com.bootdo.goods.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.AppR;
import com.bootdo.goods.domain.TGoodsAllocationAloneDO;
import com.bootdo.goods.domain.dto.GoodsAllocationDto;
import com.bootdo.goods.service.TGoodsAllocationAloneService;
import com.bootdo.provider.domain.GoodsInfoDO;
import com.bootdo.provider.service.GoodsInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.goods.service.TGoodsAllocationService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 货位表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-24 14:13:19
 */
 
@Controller
@RequestMapping("/goods/tGoodsAllocation")
public class TGoodsAllocationController {
	@Autowired
	private TGoodsAllocationService tGoodsAllocationService;
	@Autowired
	private GoodsInfoService goodsInfoService;
	@Autowired
	private TGoodsAllocationAloneService tGoodsAllocationAloneService;
	
	@GetMapping()
//	@RequiresPermissions("goods:tGoodsAllocation:tGoodsAllocation")
	String TGoodsAllocation(@RequestParam(required = false) Long providerId,Model model){
		System.out.println("providerId:"+providerId);
		model.addAttribute("providerId",providerId);
	    return "goods/tGoodsAllocation/tGoodsAllocation";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("goods:tGoodsAllocation:tGoodsAllocation")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TGoodsAllocationDO> tGoodsAllocationList = tGoodsAllocationService.list(query);
		int total = tGoodsAllocationService.count(query);
		PageUtils pageUtils = new PageUtils(tGoodsAllocationList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "goods/tGoodsAllocation/add";
	}

	@GetMapping("/edit/{allocationId}")
	String edit(@PathVariable("allocationId") Long allocationId,Model model){
		TGoodsAllocationDO tGoodsAllocation = tGoodsAllocationService.get(allocationId);
		model.addAttribute("tGoodsAllocation", tGoodsAllocation);
	    return "goods/tGoodsAllocation/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( TGoodsAllocationDO tGoodsAllocation){
		if(tGoodsAllocationService.save(tGoodsAllocation)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( TGoodsAllocationDO tGoodsAllocation){
		tGoodsAllocationService.update(tGoodsAllocation);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long allocationId){
		if(tGoodsAllocationService.remove(allocationId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] allocationIds){
		tGoodsAllocationService.batchRemove(allocationIds);
		return R.ok();
	}




	@ResponseBody
	@RequestMapping(value = "/listAllocation")
	public List<TGoodsAllocationDO> listAllocation(@RequestParam Map<String, Object> params){
		//查询列表数据

		List<TGoodsAllocationDO> tGoodsAllocationList = tGoodsAllocationService.list(params);

		return tGoodsAllocationList;
	}


    /*      我的货位     */


	@GetMapping("/edit/my/{allocationId}")
	String editMy(@PathVariable("allocationId") Long allocationId,Model model){
		TGoodsAllocationDO tGoodsAllocation = tGoodsAllocationService.get(allocationId);
		model.addAttribute("tGoodsAllocation", tGoodsAllocation);
		return "goods/tGoodsAllocation/my/edit";
	}

	@GetMapping("getGoods")
	@ResponseBody
	public PageUtils getGoods(@RequestParam Long allocationId){
		TGoodsAllocationDO tGoodsAllocation = tGoodsAllocationService.get(allocationId);
		List<GoodsAllocationDto> list = new ArrayList<>();
        if(tGoodsAllocation.getAllocationType().equals("122002")||tGoodsAllocation.getAllocationType().equals("122001")){
            list= tGoodsAllocationService.getAllocationGoods(allocationId);
		}else {
             list =tGoodsAllocationService.getAllocationAloneGoods(allocationId);
		}
		PageUtils pageUtils = new PageUtils(list, 1000);
		return pageUtils;
	}



}
